package com.dldata.display.control.Table;

import com.dldata.display.entity.TabDefineEntity;
import com.dldata.display.entity.TabImportRecordEntity;
import com.dldata.display.entity.TabStructureEntity;
import com.dldata.display.service.Table.ExcelRecordService;
import com.dldata.display.service.Table.TabDefineService;
import com.dldata.display.service.Table.TabStructureService;
import com.dldata.display.utils.StringUtil;
import com.dldata.display.utils.Util;
import net.sf.json.JSONSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by 倪继文 on 2016/8/31.
 */
@Controller
@RequestMapping(value = "/ExcelImportRecord")
public class ExcelImportRecordController {

    @Autowired
    ExcelRecordService excelRecordService;

    @Autowired
    TabStructureService tabStructureService;

    @Autowired
    TabDefineService tabDefineService;
    @Autowired
    JdbcTemplate jdbcTemplate;

    @RequestMapping(value = "/toRecord")
    public String toRecord(@RequestParam("GUID") String tableId, Model model) {

        model.addAttribute("tableId", tableId);
        return "table/ExcelImportRecord";

    }


    @RequestMapping(value = "/getRecordByTableId")
    @ResponseBody
    public List<TabImportRecordEntity> getRecordByTableId(@RequestParam("tableId") String tableId, Model model) {
        List<TabImportRecordEntity> recordEntityList = new ArrayList<TabImportRecordEntity>();

        recordEntityList = excelRecordService.getRecordByTableId(tableId);
        return recordEntityList;
    }

    @RequestMapping(value = "/getRecordByTableIdBytime")
    @ResponseBody
    public List<TabImportRecordEntity> getRecordByTableIdBytime(@RequestParam("tableId") String tableId, Model model
            , String startTime, String endTime) {
        List<TabImportRecordEntity> recordEntityList = new ArrayList<TabImportRecordEntity>();
        Timestamp s = Timestamp.valueOf(startTime + "-01 00:00:00");
        Timestamp e = Timestamp.valueOf(endTime + "-31 23:59:59");

        recordEntityList = excelRecordService.getRecordByTableIdBytime(tableId, s, e);
        return recordEntityList;
    }

    @RequestMapping(value = "/loadData")
    @ResponseBody
    public String loadData(@RequestParam("recordId") String recordId,
                           @RequestParam("tableId") String tableId,
                           @CookieValue(value = "userid", defaultValue = "") String userid) {

        //先得到表名

        TabDefineEntity tabDefineEntity = null;

        tabDefineEntity = tabDefineService.getEntityByTableId(tableId);
        String wdms = "";
        List wdmrows = null;
        String wdmsql = tabDefineEntity.getTableSql();
        if (Util.checkNotNull(wdmsql)) {
            int uindex = wdmsql.indexOf("#userid");
            if (uindex != 0) {
                wdmsql = wdmsql.replace("#userid", userid);
            }

            try {
                wdmrows = jdbcTemplate.queryForList(wdmsql);
                for (int i = 0; i < wdmrows.size(); i++) {                   //遍历
                    Map userMap = (Map) wdmrows.get(i);
                    if (i == wdmrows.size() - 1) {
                        wdms += "'" + (String) userMap.get("dm") + "'";
                    } else {
                        wdms += "'" + (String) userMap.get("dm") + "'" + ",";
                    }

                }
            } catch (Exception ex) {

            }
        }
        //再得到表数据
        //List<TabStructureEntity> tabStructureEntityList = new ArrayList<TabStructureEntity>();

        //tabStructureEntityList = tabStructureService.getStructure(tableId);

        //构建Json数据返回给界面


        String sql = "select * from " + tabDefineEntity.getName() + " where record_id='" + recordId + "'";

        if (!wdms.equals("")) {
            sql += " and wdm in (" + wdms + ")";
        }
        List rows = jdbcTemplate.queryForList(sql); //得到装有map的list
        for (int i = 0; i < rows.size(); i++) {
            Map<String, Object> map = (Map) rows.get(i);
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                entry.getValue();
                entry.setValue(entry.getValue().toString());
            }
        }
        //加入维名称列
        if (wdmrows != null) {
            for (int i = 0; i < rows.size(); i++) {
                Map<String, String> map = (Map) rows.get(i);

                map.put("wmc", "");
                for (int w = 0; w < wdmrows.size(); w++) {
                    Map wdmmap = (Map) wdmrows.get(w);
                    if (map.get("wdm").equals(wdmmap.get("dm"))) {
                        map.put("wmc", wdmmap.get("name").toString());
                        break;
                    }
                }
            }
        }
        String json = JSONSerializer.toJSON(rows).toString();

        return json;
    }


    @RequestMapping(value = "/getStructure")
    @ResponseBody
    public List<TabStructureEntity> getStructure(@RequestParam("tableId") String tableId) {
        List<TabStructureEntity> tabStructureEntities = tabStructureService.getStructure(tableId);
        //判断是否有维sql
        TabDefineEntity tabDefineEntity = tabDefineService.getEntityByID(tableId);
        if (tabDefineEntity.getTableSql() != null && tabDefineEntity.getTableSql().trim().length() > 0) {
            //有维代码则新加入一列 维名称
            TabStructureEntity TabStructureEntity1 = new TabStructureEntity();
            TabStructureEntity1.setId(UUID.randomUUID().toString());
            TabStructureEntity1.setBz("");
            TabStructureEntity1.setColLength(100);
            TabStructureEntity1.setIsKey(false);
            TabStructureEntity1.setName("wmc");
            TabStructureEntity1.setPym("WMC");
            TabStructureEntity1.setStatus(1);
            TabStructureEntity1.setTableId(tableId);
            TabStructureEntity1.setZhName("维名称");
            TabStructureEntity1.setXh(0);
            tabStructureEntities.add(0, TabStructureEntity1);
        }
        return tabStructureEntities;
    }


    @RequestMapping(value = "/checkDataIsExist")
    public void checkDataIsExist(String inputDate, String tableId, HttpServletResponse response) throws IOException {

        String result = "";

        inputDate += " 00:00:00";
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        ts = Timestamp.valueOf(inputDate);


        List<TabImportRecordEntity> tabImportRecordEntityList = excelRecordService.checkByInputDateTableId(ts, tableId);

        if (tabImportRecordEntityList.size() > 0) {
            //result = "{\\\"success\\\":false,\\\"msg\\\":\\\"系统错误，请稍候再试！\\\"\"+ ex.getMessage().replace(\"\\r\",\"\").replace(\"\\n\",\"\")+\"}\";
            TabImportRecordEntity entity = tabImportRecordEntityList.get(0);
            if (entity.getIsSh()) {
                result = "{\"isExist\":true,\"isSH\":true,\"msg\":\"当月数据已导入,并已审核通过！\"}";
            } else {
                result = "{\"isExist\":true,\"isSH\":false,\"msg\":\"当月数据已导入！\"}";
            }
        } else {
            result = "{\"isExist\":false,\"msg\":\"！\"}";
        }
        StringUtil.writeToWeb(result, "html", response);
        //return result;
    }


    @RequestMapping(value = "/deleteByInputDateTableId")
    public void deleteByInputDateTableId(String inputDate, String tableId, HttpServletResponse response) throws IOException {


        inputDate += " 00:00:00";
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        ts = Timestamp.valueOf(inputDate);
        excelRecordService.deleteByInputDateTableId(ts, tableId);

        String result = "{\"success\":true,\"msg\":\"当月数据删除成功！\"}";
        StringUtil.writeToWeb(result, "html", response);
    }


    @RequestMapping(value = "/deleteImportData")
    public void deleteImportDate(String recordId, HttpServletResponse response) throws IOException {
        String result = "";
        TabImportRecordEntity tabImportRecordEntity = excelRecordService.findOne(recordId);

        if (tabImportRecordEntity.getIsSh()) {
            result = "{\"success\":false,\"msg\":\"导入数据已审核，请先取消审核！\"}";
        } else {
            String tableId = tabImportRecordEntity.getTableId();
            excelRecordService.deleteOne(tabImportRecordEntity);

            TabDefineEntity tabDefineEntity = tabDefineService.getEntityByTableId(tableId);

            String sql = "delete from " + tabDefineEntity.getName() + " where record_id='" + recordId + "'";

            jdbcTemplate.execute(sql);

            result = "{\"success\":true,\"msg\":\"导入数据删除成功！\"}";
        }
        StringUtil.writeToWeb(result, "html", response);

    }


    @RequestMapping(value = "/checkImportData")
    public void checkImportData(String recordId, HttpServletResponse response) throws IOException {
        String result = "";
        TabImportRecordEntity tabImportRecordEntity = excelRecordService.findOne(recordId);

        if (tabImportRecordEntity.getIsSh()) {
            result = "{\"success\":true,\"msg\":\"取消数据审核成功！\"}";
        } else {
            result = "{\"success\":true,\"msg\":\"审核数据成功！\"}";
        }
        tabImportRecordEntity.setIsSh(!tabImportRecordEntity.getIsSh());

        excelRecordService.save(tabImportRecordEntity);

        StringUtil.writeToWeb(result, "html", response);
    }


}
